package com.ruoyi.web.controller.system;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.ruoyi.common.annotation.RateLimiter;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.facade.BCommentsFacade;
import com.ruoyi.system.facade.BPostsFacade;
import com.ruoyi.system.facade.VisitorFacade;
import com.ruoyi.system.service.*;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 博客API
 */
@RestController
@RequestMapping("/blog")
public class BlogController extends BaseController {
    @Autowired
    private BPostsFacade bPostsFacade;
    @Autowired
    private BCommentsFacade bCommentsFacade;
    @Autowired
    private IBCommentsService commentsService;
    @Autowired
    private IBPostTagsService postTagsService;
    @Autowired
    private IBPostsService postsService;
    @Autowired
    private IBAboutService aboutService;
    @Autowired
    private IBMessageService messageService;
    @Autowired
    private ISysConfigService configService;
    @Autowired
    private VisitorFacade visitorFacade;

    /**
     * 查询最新文章列表
     */
    @GetMapping("/getNews")
    public AjaxResult getNews()
    {
        List<BPosts> list = bPostsFacade.selectBPostsNews();
        return success(list);
    }

    /**
     * 文章详情
     * @param id
     * @return
     */
    @GetMapping(value = "/article/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bPostsFacade.selectBPostsById(id));
    }

    /**
     * 查询评论列表
     */
    @GetMapping("/comments/{id}")
    public AjaxResult getComments(@PathVariable("id") Long id)
    {
        BComments param = new BComments();
        param.setPostId(id);
        param.setcStatus(1);
        List<BComments> parents = commentsService.selectBCommentsList(param);
        List<BComments> childs = bCommentsFacade.getChilds(parents);
        if(!childs.isEmpty()) {
            for (BComments parent : parents) {
                List<BComments> comments = bCommentsFacade.getComments(childs, parent.getId());
                parent.setChildren(comments);
            }
        }
        AjaxResult result = new AjaxResult();
        result.put("count",parents.size()+childs.size());
        result.put("data",parents);
        return result;
    }

    /**
     * 添加评论
     */
    @PostMapping("/addComment")
    public AjaxResult add(@RequestBody BComments bComments)
    {
        if(StringUtils.isEmpty(bComments.getAuthorName())){
            int randomNum = (int)(Math.random() * 9000) + 1000;
            bComments.setAuthorName("匿名"+randomNum);
        }
        bComments.setRemark(IpUtils.getIpAddr());
        return toAjax(commentsService.insertBComments(bComments));
    }

    /**
     * 获取标签列表
     * @return
     */
    @GetMapping("/tags")
    public AjaxResult tags()
    {
        List<BPostTags> bPostTags = postTagsService.selectBPostTagsList(null);
        List<String> tags = bPostTags.stream().map(t -> t.getTag()).distinct().collect(Collectors.toList());
        return success(tags);
    }

    /**
     * 获取文章列表
     * @return
     */
    @GetMapping("/pagePosts")
    public TableDataInfo selectListByTag(BPosts posts){
        startPage();
        posts.setpStatus(1L);
        List<BPosts> list = postsService.selectListByTag(posts);
        TableDataInfo data = getDataTable(list);
        bPostsFacade.selectListByTag(data);
        return data;
    }

    /**
     * 获取关于信息
     * @return
     */
    @GetMapping("/selectAbout")
    public AjaxResult selectAbout(){
        BAbout about = null;
        List<BAbout> bAbouts = aboutService.selectBAboutList(null);
        if(!bAbouts.isEmpty()){
            about = bAbouts.get(0);
            /** 记录来访信息 */
            visitorFacade.record();
        }
        return success(about);
    }

    /**
     * 留言
     * @return
     */
    @PostMapping("/message")
    public AjaxResult addMessage(@RequestBody BMessage message){
        message.setRemark(IpUtils.getIpAddr());
        return toAjax(messageService.insertBMessage(message));
    }

    /**
     * 查询评论区开关参数
     */
    @GetMapping(value = "/isComments")
    public AjaxResult isComments()
    {
        return success(configService.selectConfigByKey("blog.comments"));
    }

    /**
     * 查询留言开关参数
     */
    @GetMapping(value = "/isMessage")
    public AjaxResult isMessage()
    {
        return success(configService.selectConfigByKey("blog.message"));
    }
}
